package chasegame;

import java.util.ArrayList;

import game.GameMap;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;

public class ChaseGame extends BasicGame implements MouseListener {
	ArrayList<Chaser> chasers;
	GameMap map;
	long step = 0;
	int width = 640, height = 480;
	Image chaserImage;

	public ChaseGame(String title) {
		super(title);
	}

	@Override
	public void render(GameContainer arg0, Graphics g) throws SlickException {
		g.drawImage(new Image("res/bg1.png"), 0, 0);
		for(Chaser c: chasers){
			g.rotate((float) c.getX()+16,(float) c.getY()+16,(float) c.getAngle());
			g.drawImage(chaserImage, c.getX(), c.getY());
			g.resetTransform();
		}
		for(int x = 0; x < width; x++){
			for(int y = 0; y < height; y++){
				if(this.map.hasPointAtLocation(x, y)){
					g.drawRect(x, y, 1, 1);
				}
			}
		}
		g.drawLine(0, 0, width, 0);
		g.drawLine(0, 0, 0, height);
		g.drawLine(width, 0, width, height);
		g.drawLine(0, height, width, height);
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
		chasers = new ArrayList<Chaser>();
		for(int i = 0; i < 15; i++){
			int rX = ((int) (Math.random()*width));
			int rY = ((int) (Math.random()*height));
			//Make sure they stay in the map width and height
			rX = rX -32;
			rY = rY -32;
			if(rX < 0) rX = 0;
			if(rY < 0) rY = 0;
			chasers.add(new Chaser(rX,rY));
		}
		chaserImage = new Image("res/frog.png");
		map = new GameMap(width, height);
		
	}
	
	public void drawDotsOnMap(int dotAmount){
		for(int i = 0; i < dotAmount; i++){
			int x = ((int) (Math.random()*width));
			int y = ((int) (Math.random()*height));
			for(int xx = x; xx < x+24; xx++){
				for(int yy = y; yy < y+24; yy++){
					map.setPointOnMap(xx,yy,1);
				}
			}
		}
	}

	@Override
	public void update(GameContainer arg0, int arg1) throws SlickException {
		for(Chaser c: chasers){
			c.collisionCheck(chasers);
			c.chase(map);
			c.move(map);
		}
		step++;
	}
	
	@Override
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		map.clearMap();
		map.setPointOnMap(newx, newy, 1);
		if(newx < 0 && newx > width && newy < 0 && newy > height){
			map.clearMap();
		}
	}
	
	@Override
	public void mousePressed(int button, int x, int y){
		if(button == Input.MOUSE_LEFT_BUTTON){
			map.clearMap();
			this.drawDotsOnMap(2);
		}else{
			map.clearMap();
		}
	}
	
	public static void main(String[] args){
		try { AppGameContainer app = new AppGameContainer(new ChaseGame("Chase Game")); app.setTargetFrameRate(60); app.start();  }
		catch (SlickException e) { e.printStackTrace(); }
	}
}
